from rest_framework import serializers
from django_redis import get_redis_connection
class RegisterSmsCodeSerializer(serializers.Serializer):
    text = serializers.CharField(label='图片验证码',max_length=4,min_length=4,required=True)
    image_code_id = serializers.UUIDField(label='uuid')
    def validate(self, attrs):
        #   #获取用户提交的数据
        text = attrs.get('text')
        #     获取redis的数据
        #     2.连接ｒｅｄｉｓ
        redis_conn = get_redis_connection('code')
        #    获取数据
        image_code_id = attrs.get('image_code_id')
        redis_text = redis_conn.get('img_%s'%image_code_id)
        #     2.3判断数据是否存在
        if redis_text is None:
            raise serializers.ValidationError('图片验证码已过期')
        #对比
        if redis_text.decode().lower() != text.lower():
            raise serializers.ValidationError('输入不一致')
        return attrs






